package me.lc.linklinkchrist.sprite;

import me.lc.linklinkchrist.R;
import me.lc.linklinkchrist.core.GameManager;
import me.lc.linklinkchrist.core.GameSystem;
import me.lc.linklinkchrist.core.ShopManager;
import me.lc.linklinkchrist.core.StageManager;
import me.lc.linklinkchrist.scene.GameScene;
import me.lc.linklinkchrist.scene.MainScene;
import me.lc.linklinkchrist.util.ADHelper;
import me.lc.linklinkchrist.util.AudioHelper;
import me.lc.linklinkchrist.util.GameUtil;
import android.app.Activity;

import com.wiyun.engine.nodes.Button;
import com.wiyun.engine.nodes.Label;
import com.wiyun.engine.nodes.Node;
import com.wiyun.engine.nodes.Sprite;
import com.wiyun.engine.types.WYColor3B;

public class PauseShopBlock extends Node {

	Label goldLabel;
	Button btnFinder;
	Button btnRearrange;
	Button btnTime;

	Button btnResume;
	Button btnMenu;
	Sprite bg;

	static int goldFontSize = 25;

	public PauseShopBlock() {
		bg = Sprite.make(GameSystem.pauseBGTex);
		addChild(bg);

		goldLabel = Label.make("", goldFontSize);
		float goldX = 0;
		float goldY = GameSystem.pauseBGTex.getHeight() * 0.2915f;
		goldLabel.setPosition(goldX, goldY);
		goldLabel.setColor(WYColor3B.make(210, 210, 0));
		addChild(goldLabel);

		btnFinder = Button.make(R.drawable.button_buy, R.drawable.button_buy,
				this, "onBuyFinderClicked");
		float btnX = GameSystem.pauseBGTex.getWidth() * 0.2652f;
		float btnFinderY = GameSystem.pauseBGTex.getHeight() * 0.1438f;
		btnFinder.setPosition(btnX, btnFinderY);
		btnFinder.setClickScale(1.1f);
		addChild(btnFinder);

		btnRearrange = Button.make(R.drawable.button_buy,
				R.drawable.button_buy, this, "onBuyReArrangeClicked");
		float btnArrangeY = GameSystem.pauseBGTex.getHeight() * 0.0183f;
		btnRearrange.setPosition(btnX, btnArrangeY);
		btnRearrange.setClickScale(1.1f);
		addChild(btnRearrange);

		btnTime = Button.make(R.drawable.button_buy, R.drawable.button_buy,
				this, "onBuyTimeClicked");
		float btnTimeY = GameSystem.pauseBGTex.getHeight() * (0.3988f - 0.5f);
		btnTime.setPosition(btnX, btnTimeY);
		btnTime.setClickScale(1.1f);
		addChild(btnTime);

		btnResume = Button.make(R.drawable.button_continue,
				R.drawable.button_continue_press, this, "onResumeClicked");
		float systemBtnY = (0.1f - 0.5f) * GameSystem.pauseBGTex.getHeight();
		float resumeX = (0.2637f - 0.5f) * GameSystem.pauseBGTex.getWidth();
		btnResume.setPosition(resumeX, systemBtnY);
		addChild(btnResume);

		btnMenu = Button.make(R.drawable.button_returnmenu_pause,
				R.drawable.button_returnmenu_pause_press, this,
				"onMainmenuClicked");
		float menuX = 0.2362f * GameSystem.pauseBGTex.getWidth();
		btnMenu.setPosition(menuX, systemBtnY);
		addChild(btnMenu);

	}

	public void updateGold() {
		goldLabel.setText(StageManager.instance.goldCount + "");
	}

	public void show() {
		goldLabel.setText(StageManager.instance.goldCount + "");
	}

	public void onBuyFinderClicked() {
		// 100一次
		int cost = 100;
		if (StageManager.instance.goldCount >= cost) {
			StageManager.instance.goldCount -= cost;
			StageManager.instance.saveGold();
			updateGold();
			ShopManager.instance.assertInfo
					.setFinderCount(ShopManager.instance.assertInfo
							.getFinderCount() + 1);
			saveAssertInThread();
			GameScene.make().updateTool();
			AudioHelper.buyTool();
		}
	}

	private void saveAssertInThread() {
		Thread t = new Thread() {
			@Override
			public void run() {
				ShopManager.instance.saveMyAssert();
			}
		};
		t.start();
	}

	public void onBuyReArrangeClicked() {
		int cost = 600;
		if (StageManager.instance.goldCount >= cost) {
			StageManager.instance.goldCount -= cost;
			StageManager.instance.saveGold();
			updateGold();
			ShopManager.instance.assertInfo
					.setReArrangeCount(ShopManager.instance.assertInfo
							.getReArrangeCount() + 1);
			saveAssertInThread();
			GameScene.make().updateTool();
			AudioHelper.buyTool();

		}
	}

	public void onBuyTimeClicked() {
		// 400一次
		int cost = 400;
		if (StageManager.instance.goldCount >= cost) {
			StageManager.instance.goldCount -= cost;
			StageManager.instance.saveGold();
			updateGold();
			ShopManager.instance.assertInfo
					.setTimerCount(ShopManager.instance.assertInfo
							.getTimerCount() + 1);
			saveAssertInThread();
			GameScene.make().updateTool();
			AudioHelper.buyTool();
		}
	}

	public void onResumeClicked() {
		((Activity) GameSystem.AndroidContext).runOnUiThread(new Runnable() {
			public void run() {
				AudioHelper.clickItem();
				GameScene.make().resumeGame();
				PauseShopBlock.this.setScale(0.5f);
			}
		});
	}

	public void onMainmenuClicked() {
		((Activity) GameSystem.AndroidContext).runOnUiThread(new Runnable() {
			public void run() {
				AudioHelper.clickItem();
				MainScene.make().showMainMenu();
				GameManager.instance.quitGame();
				GameUtil.switchSceneFlipX(MainScene.make());
				ADHelper.hideAd();
				PauseShopBlock.this.setScale(0.5f);
			}
		});
	}
}
